﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using Ataric.Ataric.Themester.Model;
using Ataric.Ataric.Themester.Model.ShareBrandDataSetTableAdapters;
using Ataric.Themester.Parser;

namespace Ataric.Themester.Model
{
    //TODO : Change name to DatabaseWrapper to make sense

    public class Database
    {
        //Using caching here.

        public static List<Area> Areas
        {
            get
            {
                List<Area> result = new List<Area>();
                AreaTableAdapter adapter = new AreaTableAdapter();
                ShareBrandDataSet.AreaDataTable data = adapter.GetData();
                
                foreach (ShareBrandDataSet.AreaRow item in data)
                {
                    
                    result.Add(new Area() { Name = item.Area, Id = item.ID });
                    

                }
                return result;
            }
        }
        //using caching here.
        public static List<Item> Items
        {
            get
            {
                List<Item> result = new List<Item>();
                ItemTableAdapter adapter = new ItemTableAdapter();
                ShareBrandDataSet.ItemDataTable data = adapter.GetData();
                foreach (ShareBrandDataSet.ItemRow item in data)
                {
                    var ruleitem = new Item()
                    {
                        Id = item.ID,
                        Name=item.Item,
                        Image= item.Image, 
                        AreaId = item.Area,
                        Style = item.Style,
                        StyleName = ExtractStyleName(item.Style)
                       
                    };

                    RuleSet match = FindRuleSet(ruleitem.StyleName, SessionManager.StaticSharepointCSSDocument);
                    if (match != null)
                    {
                        ruleitem.Style = match.ToString();
                    }

                    result.Add(ruleitem);
                        
                    //Add code to load style from sharepoint.css into Style property.
                }
                return result;
            }
        }
        public Database()
        {
           
            
        }

        public Database(string connectionString)
        {

        }

        internal static Item GetItem(int id)
        {
            Item item = null;
            ItemTableAdapter adapter = new ItemTableAdapter();
            ShareBrandDataSet.ItemDataTable row = adapter.GetItemById(id);
            if (row.Count > 0)
            {
                item = new Item() { AreaId = row[0].Area,
                    Id = row[0].ID, 
                    Name = row[0].Item, 
                    Image = row[0].Image,
                    Style = row[0].Style.Replace("\t",string.Empty),
                    StyleName = ExtractStyleName(row[0].Style)
                };

                RuleSet match = FindRuleSet(item.StyleName, SessionManager.StaticSharepointCSSDocument);
                if (match != null)
                {
                    item.Style = match.ToString();
                }

            }
            return item;
        }

        private static RuleSet FindRuleSet(string selector, CSSDocument sp)
        {
            string lwrselector = selector.ToLower();
            foreach (RuleSet rule in sp.RuleSets)
            {
                foreach (var item in rule.Selectors)
                {
                    if (string.Compare(item.ToString(), lwrselector, true) == 0) return rule;
                    else
                        foreach (var sl in item.SimpleSelectors)
                            if (string.Compare(sl.ToString(), lwrselector, true) == 0) return rule;
                }
            }
            return null;
        }

        private static string ExtractStyleName(string Style)
        {
            CSSParser parser = new CSSParser();
            CSSDocument document = parser.ParseText(Style);
            if (document != null)
                return document.RuleSets[0].Selectors[0].ToString();
            return string.Empty;
        }
    }
    [Serializable]
    public class Area
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    [Serializable]
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string AreaId { get; set; }
        public string Style { get; set; }
        public string Image { get; set; }
        public string StyleName { get; set; }
    }

}